package com.springboot.dxssglxt.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.dxssglxt.entity.SysUser;
import com.springboot.dxssglxt.service.ISysUserService;
import com.springboot.dxssglxt.util.RandomValidateCodeUtil;
import com.springboot.dxssglxt.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录控制层
 * 2020-07-08
 * author lbk
 */
@Controller
@RequestMapping("/api/login")
public class LoginController{

    private static final Logger log =  LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ISysUserService iSysUserService;



    /**
     * 系统PC平台用户登陆
     * @param request
     * @param response
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/login")
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        if(StringUtils.isEmpty(code)){
            return Result.reError("验证码不能为空");
        }

        String syscode = (String)request.getSession().getAttribute(RandomValidateCodeUtil.RANDOMCODEKEY);
        if(!syscode.equalsIgnoreCase(code)) {
            return Result.reError("验证码填写错误");
        }

        String account = request.getParameter("account");
        if(StringUtils.isEmpty(account)){
            return Result.reError("账号不能为空");
        }

        String pwd =  request.getParameter("pwd");
        if(StringUtils.isEmpty(pwd)){
            return Result.reError("密码不能为空");
        }
        String type =  request.getParameter("type");
        //进行登录认证
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("pwd",pwd);
        queryWrapper.eq("type",type);
        SysUser hjManager=this.iSysUserService.getOne(queryWrapper);
        if(hjManager==null){
            return Result.reError("账号密码错误！！！");
        }
        if(hjManager.getStatus()==0){
            return Result.reError("账号已被封禁！！！");
        }
        StpUtil.login(hjManager.getUserid());
        //注入用户session-保持session信息共享-保持session底层代码共享
        StpUtil.getSession().set(hjManager.getUserid()+"",hjManager);
        return Result.reOk("登录成功");
    }


    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo() {
        SysUser dto=this.iSysUserService.getById(StpUtil.getLoginIdAsString());
        return Result.reOk("查询成功",dto);
    }


    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping("/updateAccount")
    @ResponseBody
    public Result updateAccount(SysUser sysUser) {
        SysUser dto=this.iSysUserService.getById(StpUtil.getLoginIdAsString());
        BeanUtils.copyProperties(sysUser,dto,new String[]{"userid"});
        this.iSysUserService.updateById(dto);
        return Result.reOk("更新成功");
    }

}
